package app.fugema.shop.main.order.view;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.main.order.adapter.GroupBookingListAdapter;

import com.base.common.app.LoginUtils;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.RxTimer;
import com.base.common.utils.UIUtils;
import com.base.common.view.adapter.connector.OnItemClickInterface;
import com.base.common.view.base.BaseFragment;
import com.base.common.view.base.OnGetDataListener;
import com.base.common.viewmodel.BaseViewObserver;
import app.fugema.shop.R;
import app.fugema.shop.databinding.OrderListFmBinding;

import app.fugema.shop.main.order.model.GroupOrderResult;
import app.fugema.shop.main.order.model.SimpleGroupOrderBean;
import app.fugema.shop.main.order.vm.GroupBookingVM;

public class GroupBookingListFragment extends BaseFragment<OrderListFmBinding, GroupBookingVM> implements OnGetDataListener {

    public static GroupBookingListFragment getInstance(int type) {
        Bundle args = new Bundle();
        args.putInt("type", type);
        GroupBookingListFragment fragment = new GroupBookingListFragment();
        fragment.setArguments(args);
        return fragment;
    }


    @Override
    protected OrderListFmBinding initDataBinding(LayoutInflater inflater, ViewGroup container)
    {
        return DataBindingUtil.inflate(inflater, R.layout.order_list_fm, container, false);
    }

    private int type; //状态 -1全部  0:开团中  1:拼购成功  2拼团失败

    private GroupBookingListAdapter bookingListAdapter;
    private RxTimer rxTimer;

    private String userKey;

    @Override
    public void initView()
    {
        super.initView();


        bookingListAdapter = new GroupBookingListAdapter();
        binding.rvRecyclerView.setAdapter(bookingListAdapter);
        bookingListAdapter.setOnItemClickInterface(new OnItemClickInterface() {
            @Override
            public boolean onItemClick(View view, int onclickType, int itemType, int position, Object bean) {
                if (bean instanceof SimpleGroupOrderBean)
                {

                }
                return false;
            }
        });
        viewUtils.setRefreshStateLayout(binding.rvRecyclerView, this, this);

        if (rxTimer == null) {
            rxTimer = new RxTimer();
        } else {
            rxTimer.cancel();
        }

        rxTimer.interval(1000, new RxTimer.RxAction() {
            @Override
            public void action(long number)
            {
                if (bookingListAdapter.getChildCount() > 0)
                {
                    for (int i = 0; i < bookingListAdapter.getItemCount(); i++)
                    {
                        if (bookingListAdapter.getItemBean(i) instanceof SimpleGroupOrderBean)
                        {
                            SimpleGroupOrderBean bean = (SimpleGroupOrderBean) bookingListAdapter.getItemBean(i);
                            //bean.timeChanged();
                        }
                    }
                }
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (rxTimer != null) {
            rxTimer.cancel();
        }
    }

    @Override
    public void initData() {
        super.initData();

        userKey = LoginUtils.getToken();

        type = getArguments().getInt("type");
        onGetData(1);
    }


    @Override
    public void onGetData(int pn)
    {
        MutableLiveData<LiveDataWrapper<GroupOrderResult>> liveData = viewModel.getGroupOrders(pn, userKey);
        liveData.observeForever(new BaseViewObserver<LiveDataWrapper<GroupOrderResult>>(viewUtils.getStateLayout()) {
            @Override
            protected void onSuccess(LiveDataWrapper<GroupOrderResult> data)
            {

//                for (CommentProductResult.RecordsBean record : data.data.getData().getRecords()) {
//                    record.setStatus(3);
//                }
                viewUtils.setDataListRefreshLayout(bookingListAdapter, pn, 5, data.data.getData());
            }

            @Override
            public boolean isEmptyData(LiveDataWrapper<GroupOrderResult> data)
            {
                return pn == 1 && UIUtils.isEmpty(data.data.getData());
            }

            @Override
            public boolean isRefreshLayoutData() {
                return true;
            }

            @Override
            protected void onComplete() {
                super.onComplete();
                liveData.removeObserver(this);
            }
        });
    }


}
